<?php
class Index{
	private $data;
	// 初始化
	public function Init($IDs)
	{
		xj_log_message('debug', '['.__METHOD__.']'.'===================>');
		$this->load->database();
		$this->load->model('Dy_model');
		$this->data = array();
		$this->data["id"] = $IDs[0];
		$this->data["class"] = __CLASS__;
		$this->data["page_content"] = "index.php";
		$this->data['userid'] = 'xj_fly_userid';
		$this->data['channelid'] = 'qd0001';
		$this->data['key'] = '123456789aaa';
	}

	public function Get($IDs){
		$this->data['dy_data'] = $this->Dy_model->dy_select($this->data['userid']);
		$this->load->view("index.php", $this->data);
	}

	public function GetAdd($IDs){
		$this->load->view("add.php", $this->data);
	}

	public function GetDel($IDs){
		$ret = $this->Dy_model->dy_del($IDs[0]);
		if ($ret){
			$this->output->append('ok');
		}else{
			$this->output->append('error');
		}
	}
	public function GetMsgdel($IDs){
		$ret = $this->Dy_model->msg_del($IDs[0]);
		if ($ret){
			$this->output->append('ok');
		}else{
			$this->output->append('error');
		}
	}
	public function GetMsg($IDs){
		$this->data['dy_data'] = $this->Dy_model->msg_select($this->data['userid']);
		$this->load->view("msg.php", $this->data);
	}

	public function GetPush_msg($IDs){
		xj_show_info('method GET is error.', 'error');
	}

	public function PostPush_msg($IDs){
		$ret = TRUE;
		$json = $this->input->input_stream();
		//xj_log_message('debug', '[' . __METHOD__ . ']' . 'JSON:'.$json);
		$postArr = $json;
		xj_log_message('debug', '[' . __METHOD__ . ']' . 'postArr:'.json_encode($postArr));
		$postArr['updated'] = time();
		if ($ret)$ret = $this->Dy_model->msg_add($postArr);
		if ($ret) {
			xj_log_message('debug', '[' . __METHOD__ . ']' . 'OK');
			$this->output->append('ok');
		}else{
			$this->output->append('error');
			xj_set_status_header(500, 'Internal Server Error');
		}
	}

	public function PostAdd($IDs){
		$postArr = $this->input->post();
		$postArr['userid'] = $this->data['userid'];
		$postArr['channelid'] = $this->data['channelid'];
		ksort($postArr);
		$buf= http_build_query($postArr);
		$buf.='&key='.$this->data['key'];
		xj_log_message('debug', '['.__METHOD__.']'.'BUF:['.json_encode($buf).']');
		$postArr['sign'] = strtoupper(md5($buf));
		// 调用外围
		//$query = http_build_query($queryArray);
		$quest = 'POST';
		$possData = array(
			'title' => $this->input->value('title'),
			'type' => $this->input->value('type'),
			'content' => $this->input->value('content')
		);
		$possData = json_encode($postArr);
		$url = 'http://10.169.217.198:8089/channel_subscribe';
		$this->load->library('Curl');
		//xj_log_message('debug', '['.__METHOD__.']'.'URL:'.$url);
		$headerArr = array(
			'Content-Type' => 'application/json; charset=utf-8\', \'charset\': \'utf-8'
		);
		$ret = $this->curl->call($url, FALSE, $quest, $possData);
		$html = NULL;
		$tree = array();
		isset($ret['html']) && $html = json_decode($ret['html']);
		if (isset($html) && isset($html->RespCode) && $html->RespCode == '000000'){
			$ret = TRUE;
		}else{
			$ret = FALSE;
		}
		// 存库
		$postArr['updated'] = time();
		if ($ret)$ret = $this->Dy_model->dy_add($postArr);
		if ($ret){
			$this->data['msg_success'] = '添加成功!';
		}else{
			$this->data['msg_error'] = '添加失败！';
		}
		xj_log_message('debug', '['.__METHOD__.']'.'POST:'.json_encode($postArr));
		$this->load->view("add.php", $this->data);
	}

	public function GetFly($IDs){
		$quest = 'GET';
		$url = 'http://www.feeyo.com/flight/num/'.$IDs[0].'.htm';
		$this->load->library('Curl');
		//xj_log_message('debug', '['.__METHOD__.']'.'URL:'.$url);
		$ret = $this->curl->call($url, FALSE, $quest);
		if (!isset($ret['html'])){
			$this->output->append('');
			return FALSE;
		}
		$html = $ret['html'];
		//$this->output->append($html);
		//return TRUE;
		$pattern = '#vote.feeyo.com/airports/list/(.*?)\.html"#i';
		preg_match_all($pattern,$html,$matches);
		$matches = array_unique($matches[0]);
		$obj = array();
		foreach ($matches as $v){
			$obj[] = substr($v, 29, 3);
		}
		$this->output->append(json_encode($obj));
	}
	public function Post($IDs){
		$data = array();
		$data["title"] = "Welcome to ".$_ENV['XJ_PHP'];
		$data["id"] = $IDs[0];
		xj_load_view("index.php", $data);
	}
	public function Head($IDs){
		$data = array();
		$data["title"] = "Welcome to ".$_ENV['XJ_PHP'];
		$data["id"] = $IDs[0];
		xj_load_view("index.php", $data);
	}
	public function Put($IDs){
		$data = array();
		$data["title"] = "Welcome to ".$_ENV['XJ_PHP'];
		$data["id"] = $IDs[0];
		xj_load_view("index.php", $data);
	}	
	public function Delete($IDs){
		$data = array();
		$data["title"] = "Welcome to ".$_ENV['XJ_PHP'];
		$data["id"] = $IDs[0];
		xj_load_view("index.php", $data);
	}
}